package com.wcy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wcy.entity.User;
import com.wcy.entity.UserRole;
import com.wcy.mapper.UserRoleMapper;
import com.wcy.service.IUserRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴传亿
 * @since 2025-04-28
 */
@Service
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements IUserRoleService {
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public Boolean replaceUserRole(User user) {
        if(user.getUserId()!=null&&user.getRoles()!=null&&!user.getRoles().isEmpty()){
            userRoleMapper.delete(new QueryWrapper<UserRole>().eq("user_id",user.getUserId()));
            userRoleMapper.insert(user
                    .getRoles()
                    .stream()
                    .map(e->new UserRole(user.getUserId(),e.getRoleId()))
                    .collect(Collectors.toList()));
            return true;
        }
        return false;
    }
}
